<!--
 * @Descripttion: (供应商信息/base_supplier)
 * @Author: (admin)
 * @Date: (2025-04-27)
-->
<template>
	<div>
		<el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
			<el-form-item label="供应商编码" prop="supplierCode">
				<el-input v-model="queryParams.supplierCode" placeholder="请输入供应商编码" />
			</el-form-item>
			<el-form-item label="供应商名称" prop="supplierName">
				<el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" />
			</el-form-item>
			<el-form-item label="启用标志" prop="enabled">
				<el-select clearable v-model="queryParams.enabled" placeholder="请选择启用标志">
					<el-option v-for="item in options.enabledOptions" :key="item.dictValue" :label="item.dictLabel"
						:value="item.dictValue">
						<span class="fl">{{ item.dictLabel }}</span>
						<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.dictValue }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
				<el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="15" class="mb10">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['base:supplier:add']" plain icon="plus" @click="handleAdd">
					{{ $t('btn.add') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['base:supplier:edit']" plain icon="edit"
					@click="handleUpdate">
					{{ $t('btn.edit') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['base:supplier:delete']" plain icon="delete"
					@click="handleDelete">
					{{ $t('btn.delete') }}
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
			highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="supplierId" label="主键" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('supplierId')" />
			<el-table-column prop="supplierCode" label="供应商编码" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('supplierCode')" />
			<el-table-column prop="supplierName" label="供应商名称" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('supplierName')" />
			<el-table-column prop="enabled" label="启用标志" align="center" v-if="columns.showColumn('enabled')">
				<template #default="scope">
					<dict-tag :options="options.enabledOptions" :value="scope.row.enabled" />
				</template>
			</el-table-column>
			<el-table-column prop="country" label="国家" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('country')" />
			<el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('remark')" />
			<el-table-column label="操作" width="160">
				<template #default="scope">
					<el-button-group>
						<el-button text v-hasPermi="['base:supplier:edit']" type="success" icon="edit"
							@click="handleUpdate(scope.row)">
							{{ $t('btn.edit') }}
						</el-button>
						<el-button text v-hasPermi="['base:supplier:delete']" type="danger" icon="delete"
							@click="handleDelete(scope.row)">
							{{ $t('btn.delete') }}
						</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
			@pagination="getList" />


		<el-dialog draggable :close-on-click-modal="false" :title="title" :lock-scroll="false" v-model="open">
			<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20">
					<!-- <el-col :lg="24">
						<el-form-item label="主键" prop="supplierId">
							<el-input v-model="form.supplierId" placeholder="请输入主键" :disabled="opertype != 1" />
						</el-form-item>
					</el-col> -->
					<el-col :lg="24">
						<el-form-item label="供应商编码" prop="supplierCode">
							<el-input v-model="form.supplierCode" placeholder="请输入供应商编码" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="供应商名称" prop="supplierName">
							<el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="启用标志" prop="enabled">
							<el-radio-group v-model="form.enabled">
								<el-radio v-for="item in options.enabledOptions" :key="item.dictValue"
									:value="parseInt(item.dictValue)">
									{{ item.dictLabel }}
								</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="国家" prop="country">
							<el-input v-model="form.country" placeholder="请输入国家" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="opertype != 3">
				<el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
				<el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="basesupplier">
import {
	pageBaseSupplier,
	addBaseSupplier, delBaseSupplier,
	updateBaseSupplier, getBaseSupplier,
}
	from '@/api/baseManage/baseSupplier.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	sort: '',
	sortType: 'asc',
	supplierCode: undefined,
	supplierName: undefined,
	enabled: undefined,
})
const columns = ref([
	{ visible: false, prop: 'supplierId', label: '主键', },
	{ visible: true, prop: 'supplierCode', label: '供应商编码', },
	{ visible: true, prop: 'supplierName', label: '供应商名称', },
	{ visible: true, prop: 'enabled', label: '启用标志' },
	{ visible: true, prop: 'country', label: '国家', },
	{ visible: true, prop: 'remark', label: '备注', },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList() {
	loading.value = true
	pageBaseSupplier(queryParams).then(res => {
		const { code, data } = res
		if (code == 200) {
			dataList.value = data.result
			total.value = data.totalNum
			loading.value = false
		}
	})
}

// 查询
function handleQuery() {
	queryParams.pageNum = 1
	getList()
}

// 重置查询操作
function resetQuery() {
	proxy.resetForm("queryRef")
	handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map((item) => item.supplierId);
	single.value = selection.length != 1
	multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
	var sort = undefined
	var sortType = undefined

	if (column.prop != null && column.order != null) {
		sort = column.prop
		sortType = column.order

	}
	queryParams.sort = sort
	queryParams.sortType = sortType
	handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
	single: true,
	multiple: true,
	form: {},
	rules: {
		supplierId: [{ required: true, message: "主键不能为空", trigger: "blur" }],
		supplierCode: [{ required: true, message: "供应商编码不能为空", trigger: "blur" }],
		supplierName: [{ required: true, message: "供应商名称不能为空", trigger: "blur" }],
		enabled: [{ required: true, message: "启用标志不能为空", trigger: "change", }],
	},
	options: {
		// 启用标志 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
		enabledOptions: [
			{ dictLabel: '禁用', dictValue: '0', listClass: 'danger' },
			{ dictLabel: '启用', dictValue: '1', listClass: 'success' }
		],
	}
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
	open.value = false
	reset()
}

// 重置表单
function reset() {
	form.value = {
		supplierId: null,
		supplierCode: null,
		supplierName: null,
		enabled: null,
		country: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null,
	};
	proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
	reset();
	open.value = true
	title.value = '添加供应商信息'
	opertype.value = 1
	form.value.supplierId = proxy.parseGuid();
}
// 修改按钮操作
function handleUpdate(row) {
	reset()
	const id = row.supplierId || ids.value
	getBaseSupplier(id).then((res) => {
		const { code, data } = res
		if (code == 200) {
			open.value = true
			title.value = '修改供应商信息'
			opertype.value = 2

			form.value = {
				...data,
			}
		}
	})
}

// 添加&修改 表单提交
function submitForm() {
	proxy.$refs["formRef"].validate((valid) => {
		if (valid) {

			if (form.value.supplierId != undefined && opertype.value === 2) {
				updateBaseSupplier(form.value).then((res) => {
					proxy.$modal.msgSuccess("修改成功")
					open.value = false
					getList()
				})
			} else {
				addBaseSupplier(form.value).then((res) => {
					proxy.$modal.msgSuccess("新增成功")
					open.value = false
					getList()
				})
			}
		}
	})
}

// 删除按钮操作
function handleDelete(row) {
	const Ids = row.supplierId ? [row.supplierId] : undefined || ids.value
	proxy
		.$confirm('是否确认删除"' + Ids.length + '"条数据项？', "警告", {
			confirmButtonText: proxy.$t('common.ok'),
			cancelButtonText: proxy.$t('common.cancel'),
			type: "warning",
		})
		.then(function () {
			return delBaseSupplier(Ids)
		})
		.then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		})
}




handleQuery()
</script>